package cn.ym.org.controller;

import cn.ym.basic.annotation.PreAuthorize;
import cn.ym.basic.utils.AjaxResult;
import cn.ym.basic.utils.PageList;
import cn.ym.org.domain.Department;
import cn.ym.org.query.DepartmentQuery;
import cn.ym.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 接口类：部门
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门接口", description = "处理部门的所有请求")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    /**
     * 接口：添加或修改部门信息
     */
    @PreAuthorize(name = "添加或修改部门信息", sn = "department:save")
    @ApiOperation(value = "添加或修改部门信息")
    @PutMapping
    public AjaxResult save(@RequestBody Department department) {

        if (department.getId() == null) {
            departmentService.add(department);
        } else {
            departmentService.update(department);
        }
        return new AjaxResult();
    }

    /**
     * 接口：删除单个部门信息
     */
    @PreAuthorize(name = "删除单个部门信息", sn = "department:delete")
    @ApiOperation(value = "删除单个部门信息")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {

        departmentService.delete(id);
        return new AjaxResult();

    }

    /**
     * 接口：查询所有部门信息
     */
    @PreAuthorize(name = "查询所有部门信息", sn = "department:getAll")
    @ApiOperation(value = "查询所有部门信息")
    @GetMapping
    public List<Department> findAll() {

        return departmentService.findAll();
    }

    /**
     * 接口：查询单个部门信息
     */
    @PreAuthorize(name = "查询单个部门信息", sn = "department:getOne")
    @ApiOperation(value = "查询单个部门信息")
    @GetMapping("/{id}")
    public Department findOne(@PathVariable("id") Long id) {

        return departmentService.findOne(id);
    }


    /**
     * 接口：批量删除部门信息
     */
    @PreAuthorize(name = "批量删除部门信息", sn = "department:patchDel")
    @ApiOperation(value = "批量删除部门信息")
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids) {

        departmentService.patchDel(ids);
        return new AjaxResult();

    }

    /**
     * 接口：分页查询 + 高级查询
     */
    @PreAuthorize(name = "分页查询和高级查询", sn = "department:queryPage")
    @ApiOperation(value = "分页查询或高级查询")
    @PostMapping
    public PageList<Department> queryPage(@RequestBody DepartmentQuery departmentQuery) {
        return departmentService.queryPage(departmentQuery);
    }

    /**
     * 接口：部门树
     */
    @PreAuthorize(name = "部门树", sn = "department:deptTree")
    @ApiOperation(value = "部门树")
    @GetMapping("/deptTree")
    public List<Department> deptTree() {
        return departmentService.deptTree();
    }
}
